import { Component, ChangeDetectorRef, ChangeDetectionStrategy } from '@angular/core';
import { KeyIssuesModel } from '../../templates/keyIssues/keyIssues.model';
import { CompanyRiskRatingService } from '../../companyRiskRating.service';
import { HttpClient } from '@angular/common/http';
import { ChartColumnModel } from '../../../shared/companyIndicator/models/chartColumn.model';
import { ClusterService } from '../../../../riskCluster/cluster.service';

/**
 * Company key issues summary component
 *
 * Author: Sorin Spiru
 * Date: 16.01.2018
 */
@Component({
    selector: 'key-issues',
    templateUrl: '../../templates/keyIssues/keyIssues.component.html',
    styleUrls: [
        '../../../company.component.scss',
        '../../companyRiskRating.component.scss',
        '../../templates/keyIssues/keyIssues.component.scss'
    ],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class KeyIssuesSummaryComponent extends KeyIssuesModel {
    public subPeerGroup: string;
    public company;
    public clusterName;
    public riskClusters;
    public weightChartData;
    public currentClusterName;
    public overallExcessExposure: number = 0;

    constructor(
        private cdRef: ChangeDetectorRef,
        private riskService: CompanyRiskRatingService,
        private clusterService: ClusterService) {
        super(cdRef);

        this.currentComponent = "keyIssues";
        this.headerPercentWidth = 30;
        this.showLimit = true;
        this.columns = [
            new ChartColumnModel('Weight', 20, 20, 'finalWeight', false, '#B3D4D1', true, false, false, '%', 'assets/img/risk/icon-subindustry-avg.png', 2, true, this.formatMeiWeight),
            new ChartColumnModel('Exposure', 20, 20, 'exposure', false, '#B3D4D1', true, false, false, '', 'assets/img/risk/icon-subindustry-avg.png', 1),
            new ChartColumnModel('Managed Risk', 20, 20, 'management', false, '#B5B7B7', true, false, false, '', 'assets/img/risk/icon-subindustry-avg.png', 1),
            new ChartColumnModel('Unmanaged Risk', 30, 20, 'unmanaged', true, 'black', true, true, true, '', 'assets/img/risk/icon-subindustry-avg.png', 1),
        ];

        this.getIndicatorDetails = this.riskService.getIndicatorByCode;

        this.cdRef.markForCheck();
    }

    ngOnInit() {
        this.loaded = new Promise<void>((resolve, reject) => {
            this.riskService.company.subscribe(company => {
                this.company = company;
                this.baseline = company.issues.baseline;
                this.elements = company.issues.others;

                // add baseline to front of indicators list, but deep clone the array first so it does not backfire back to company.issues.others
                this.elements = this.elements.slice();
                this.elements.forEach(issue => {
                    if(issue.events && issue.events.length > 1){
                        issue.events.sort(function(a, b){return b.category - a.category});
                    }
                });
                this.elements.unshift(this.baseline);
                this.subPeerGroup = company.subPeerGroup;

                // format data for weight chart
                this.weightChartData = this.elements.map((element) => {
                    return {
                        name: element.name,
                        exposure: element.exposure.value,
                        management: element.management.value,
                        finalWeight: element.finalWeight * 100
                    };
                });
                this.cdRef.markForCheck();
                resolve();

                this.clusterService.mei.subscribe(clusters => {
                    this.riskClusters = clusters;
                    this.cdRef.markForCheck();
                });
            });
        });
    }

    public toggleIndicatorsDisplay(): void {
        this.showAllIndicators = !this.showAllIndicators;
    }

    public formatMeiWeight(value: number): number {
        return value * 100;
    }

    private getIndicatorCluster(indicator) {
        return this.riskClusters.filter((cluster) => {
            return indicator.unmanaged.clusterName === cluster.name;
        })[0];
    }

    public getIndicatorClusterProp(indicator, propName) {
        return this.getIndicatorCluster(indicator)[propName];
    }
}
